Note: There are often multiple ways to answer each question.
ggplot2
, dplyr
and maps
packages and save the world map data frame into the variable map_world
. (Hint: Look at this PDF for hints on where the world map data frame is saved.)library(ggplot2)
library(dplyr)
library(maps)
map_world <- map_data("world")
ggplot(map_world) +
geom_polygon(aes(x = long, y = lat, group = group),
col = "black", fill = "white") +
coord_quickmap()
map_china
. Plot with the same settings as above, except that in addition, we don’t see the latitude and longitude axes.map_china <- map_world %>% filter(region == "China")
map_theme <- theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank()
)
ggplot(map_china) +
geom_polygon(aes(x = long, y = lat, group = group),
col = "black", fill = "white") +
coord_quickmap() +
map_theme
map_state
. Plot just the states of California, Oregon and Washington.map_state <- map_data("state")
map_state %>%
filter(region %in% c("california", "oregon", "washington")) %>%
ggplot() +
geom_polygon(aes(x = long, y = lat, group = group)) +
coord_quickmap()
datasets
package. (The dataset
package contains variables state.name
and state.division
which we will use.) Create a data frame named division
such that the first column region
contains state.name
in lower case, and the second column division
contains state.division
.library(datasets)
division <- data.frame(region = tolower(state.name), division = state.division)
head(division)
## region division
## 1 alabama East South Central
## 2 alaska Pacific
## 3 arizona Mountain
## 4 arkansas West South Central
## 5 california Pacific
## 6 colorado Mountain
division
to map_state
(with map_state
on the left) on the key region
.map_state2 <- map_state %>% left_join(division, by = "region")
## Warning: Column `region` joining character vector and factor, coercing into
## character vector
head(map_state2)
## long lat group order region subregion division
## 1 -87.46201 30.38968 1 1 alabama <NA> East South Central
## 2 -87.48493 30.37249 1 2 alabama <NA> East South Central
## 3 -87.52503 30.37249 1 3 alabama <NA> East South Central
## 4 -87.53076 30.33239 1 4 alabama <NA> East South Central
## 5 -87.57087 30.32665 1 5 alabama <NA> East South Central
## 6 -87.58806 30.32665 1 6 alabama <NA> East South Central
ggplot(map_state2) +
geom_polygon(aes(x = long, y = lat, group = region, fill = division)) +
coord_quickmap()
The NAs are due to the fact that the District of Columbia does not appear in the division
dataset. We can remove all rows related to the District of Columbia to get rid of the NA:
map_state2 %>% filter(!is.na(division)) %>%
ggplot() +
geom_polygon(aes(x = long, y = lat, group = region, fill = division)) +
coord_quickmap()
state.abb
and state.center
to help you. (Hint: Combine state.abb
and state.center
into an auxiliary data frame first.)state_name <- data.frame(abb = state.abb, x = state.center$x, y = state.center$y)
ggplot() +
geom_polygon(data = map_state2,
aes(x = long, y = lat, group = region, fill = division)) +
geom_text(data = state_name, aes(x = x, y = y, label = abb)) +
coord_quickmap()
state_name2 <- state_name %>% filter(abb != "HI" & abb != "AK")
ggplot() +
geom_polygon(data = map_state2,
aes(x = long, y = lat, group = region, fill = division)) +
geom_text(data = state_name2, aes(x = x, y = y, label = abb)) +
coord_quickmap()